<?php
/**
 * Procesos Model for Procesos Component
 *
 * @package    ZezaTech
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
 */

// No direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );
jimport( 'joomla.methods' );
require_once(JPATH_COMPONENT.DS.'persistance'.DS.'impl'.DS.'DAOFactoryImpl.php');
require_once(JPATH_COMPONENT.DS.'persistance'.DS.'impl'.DS.'DAOLangImpl.php');
require_once(JPATH_COMPONENT.DS.'rsc'.DS.'RscES.php');
require_once(JPATH_COMPONENT.DS.'rsc'.DS.'RscEU.php');
/**
 * Procesos Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class HizkuntzakModelHizkuntzak extends JModel
{
	function getLangNumbers(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$nDao=$factoria->getDAONumber();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}
		else
		$_SESSION['hizkLang'] = $code;

		$result=$nDao->getLangNumber($con, $code, 0);

		if(count($result)==0){
			for($i=1; $i<13; $i++){
				$dtoNumb = $factoria->createDTONumber();
				$dtoNumb->setIsoCode($code);
				$dtoNumb->setNumber($i);
				$result[]= $dtoNumb ;
			}
		}

		return $result;
	}

	function getNumbers(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$nDao=$factoria->getDAONumber();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}
		else
		$_SESSION['hizkLang'] = $code;

		$result=$nDao->getLangNumber($con, $code, 1);

		return $result;
	}

	function getSongs(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$sDao=$factoria->getDAOSong();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}
		else
		$_SESSION['hizkLang'] = $code;

		$result=$sDao->getLangSongs($con, $code, 1);

		return $result;
	}

	function getLangSongs(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$sDao=$factoria->getDAOSong();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}
		else
		$_SESSION['hizkLang'] = $code;

		$result=$sDao->getLangSongs($con, $code, 0);

		return $result;
	}

	/*
	 *  Hizkuntza baten hitzen informazioa lortzeko funtzioa
	*/
	function getLangWords(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$iDao=$factoria->getDAOInfo();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}
		else
		$_SESSION['hizkLang'] = $code;

		$result=$iDao->getLangInfo($con, $code, 0);

		if(count($result)==0){
			$wDao=$factoria->getDAOWord();
			$result = $wDao->getWords($con, 1);
		}

		return $result;
	}

	/*
	 *  Hizkuntza baten hitzen informazioa lortzeko funtzioa
	*/
	function getLangQasEdit(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$lqaDao=$factoria->getDAOLangQa();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}
		else
		$_SESSION['hizkLang'] = $code;

		$result=$lqaDao->getLangQa($con, $code, 0);

		if(count($result)==0){
			$qaDao=$factoria->getDAOQa();
			$qas = $qaDao->getQas($con, 1);
			foreach($qas as $qa){
				$qaLang = $factoria->createDTOLangQa();
				$qaLang->setLang($this->getLang());
				$qaLang->setQa($qa);
				$result[] = $qaLang;
			}
		}

		return $result;
	}

	/*
	 *  Hizkuntza baten hitzen informazioa lortzeko funtzioa
	*/
	function getLangInfo(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$iDao=$factoria->getDAOInfo();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}
		else
		$_SESSION['hizkLang'] = $code;

		return $iDao->getLangInfo($con, $code, 1);
	}

	/*
	 *  Hizkuntza baten hitzen informazioa lortzeko funtzioa
	*/
	function getLangQas(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$lqaDao=$factoria->getDAOLangQa();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}
		else
		$_SESSION['hizkLang'] = $code;

		return $lqaDao->getLangQa($con, $code, 1);
	}

	function getWord(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$wDao=$factoria->getDAOWord();

		$id = JRequest::getString('idWord');

		$result = $wDao->getWord($con, $id);

		if($result == null){
			$result = $factoria->createDTOWord();
		}

		return $result;
	}

	function getQa(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$qaDao=$factoria->getDAOQa();

		$id = JRequest::getString('id');

		$result = $qaDao->getQa($con, $id);

		if($result == null){
			$result = $factoria->createDTOQa();
		}

		return $result;
	}

	function getLang(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$lDao=$factoria->getDAOLang();

		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}

		$result = $lDao->getLang($con, $code);

		return $result;
	}

	function getSong(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$lDao=$factoria->getDAOSong();

		$id = JRequest::getString('id');
		$code = JRequest::getString('hlang');

		if($code == null || $code == ''){
			$code = $_SESSION['hizkLang'];
		}

		$result = $lDao->getSong($con, $code, $id);

		if($result == null){
			$result = $factoria->createDTOSong();
		}

		return $result;
	}

	/*
	 * Hitz berri bat sartzeko funtzioa
	*/
	function addWordsLang(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$wDao=$factoria->getDAOWord();
		$words = $wDao->getWords($con, 1);

		$iDao=$factoria->getDAOInfo();
		$lang = JRequest::getString('hlang');

		foreach($words as $w){
			$dtoInfo = $factoria->createDTOInfo();
				
			$dtoInfo->setIsoCode($lang);
			$dtoInfo->setIdWord($w->getIdWord());
			$dtoInfo->setSpelling(JRequest::getString('w'.$w->getIdWord().'-s'));
			$dtoInfo->setPronoun(JRequest::getString('w'.$w->getIdWord().'-p'));
			//Tratamiento fichero
				
			$dtoInfo->setVideoPath($this->treatVideoFile('w',$w->getIdWord(), $lang));
				
			//Egoera kudeatu
			$active = JRequest::getBool('w'.$w->getIdWord().'-a');
			if(!$active){
				$active = 0;
			}
			$dtoInfo->setActive($active);
				
			//Sartu DB-n
			if(!$iDao->addInfo($con, $dtoInfo)){

				return false;
			}
		}

		return true;

	}

	function addQasLang(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$qaDao=$factoria->getDAOQa();
		$qas = $qaDao->getQas($con, 1);

		$lqaDao=$factoria->getDAOLangQa();
		$lang = $this->getLang();

		foreach($qas as $qa){
			$dto = $factoria->createDTOLangQa();
				
			$dto->setLang($lang);
			$dto->setQa($qa);
			$dto->setQSpelling(JRequest::getString('q'.$qa->getId().'-s'));
			$dto->setQPronoun(JRequest::getString('q'.$qa->getId().'-p'));
			$dto->setASpelling(JRequest::getString('a'.$qa->getId().'-s'));
			$dto->setAPronoun(JRequest::getString('a'.$qa->getId().'-p'));
			//Tratamiento fichero
				
			$dto->setQVideoPath($this->treatVideoFile('q',$qa->getId(), $lang->getIsoCode()));
			$dto->setAVideoPath($this->treatVideoFile('a',$qa->getId(), $lang->getIsoCode()));
			//Egoera kudeatu
			$active = JRequest::getBool('qa'.$qa->getId().'-a');
			if(!$active){
				$active = 0;
			}
			$dto->setActive($active);
				
			//Sartu DB-n
			if(!$lqaDao->addLangQa($con, $dto)){

				return false;
			}
		}

		return true;

	}

	/*
	 * Hitz berri bat sartzeko funtzioa
	*/
	function addNumbersLang(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$nDao=$factoria->getDAONumber();

		$lang = JRequest::getString('hlang');

		for($i=1; $i<13; $i++){
			$dtoNumber = $factoria->createDTONumber();
				
			$dtoNumber->setIsoCode($lang);
			$dtoNumber->setNumber($i);
			$dtoNumber->setSpelling(JRequest::getString('n'.$i.'-s'));
			$dtoNumber->setPronoun(JRequest::getString('n'.$i.'-p'));
			//Tratamiento fichero
				
			$dtoNumber->setAudioPath($this->treatAudioFile('n'.$i.'-v','audio', $lang));
			//Egoera kudeatu
			$active = JRequest::getBool('n'.$i.'-a');
			if(!$active){
				$active = 0;
			}
			$dtoNumber->setActive($active);
				
			//Sartu DB-n
			if(!$nDao->addNumber($con, $dtoNumber)){

				return false;
			}
		}

		return true;

	}
	
	/*
	 * Igotako fitxategiak formateatzeko funtzioa ffmpeg erabiliaz
	 * webm formatutik mp4, ogg eta flv-ra
	 */
	
	function formatFile($path, $name, $inext){
		
		if (file_exists(getcwd().$path.$name.".mp4")){
			chmod(getcwd().$path.$name.".mp4", 0644);
			unlink(getcwd().$path.$name.".mp4");
		}
		$command = "ffmpeg -i ".getcwd().$path.$name.$inext." ".getcwd().$path.$name.".mp4 &";
		$convert = exec($command);
		if($convert){
			echo("MP4 convertion failed...\n");
		}
		
		if (file_exists(getcwd().$path.$name.".ogg")){
			chmod(getcwd().$path.$name.".ogg", 0644);
			unlink(getcwd().$path.$name.".ogg");
		}
		$command = "ffmpeg -i ".getcwd().$path.$name.$inext." ".getcwd().$path.$name.".ogg &";
		$convert = exec($command);
		if($convert){
			echo("OGG convertion failed...\n");
		}
		
		if (file_exists(getcwd().$path.$name.".flv")){
			chmod(getcwd().$path.$name.".flv", 0644);
			unlink(getcwd().$path.$name.".flv");
		}
		$command = "ffmpeg -i ".getcwd().$path.$name.$inext." ".getcwd().$path.$name.".flv &";
		$convert = exec($command);
		if($convert){
			echo("FLV convertion failed...\n");
		}
	}

	/*
	 * Fitxategi igoera kudeatzeko funtzioa
	*/

	function treatVideoFile($name, $idWord, $lang){
		$option=JRequest::getCmd('option');

		$o_path = JRequest::getString($name.$idWord.'-vo');
		$i_name = $name.$idWord.'-v';
		$path = 'components'.DS.$option.DS.'uploads'.DS;

		if (($_FILES[$i_name]["size"] < 15000000) && ($_FILES[$i_name]["name"] != "")){

			if ($_FILES[$i_name]["error"] > 0){
				echo "Return Code: " . $_FILES[$i_name]["error"] . "<br />";
				return $o_path;
			}
			else{
				/*
				 echo "Upload: " . $_FILES["$i_name"]["name"] . "<br />";
				echo "Type: " . $_FILES["$i_name"]["type"] . "<br />";
				echo "Size: " . ($_FILES["$i_name"]["size"] / 1024) . " Kb<br />";
				echo "Temp file: " . $_FILES["$i_name"]["tmp_name"] . "<br />";
				*/
				if (file_exists($path . $name.$lang."_".$idWord.'.webm')){
					chmod($path .$name. $lang."_".$idWord.'.webm', 0644);
					unlink($path .$name. $lang."_".$idWord.'.webm');
				}
				move_uploaded_file($_FILES[$i_name]["tmp_name"],
				$path .$name. $lang."_".$idWord.'.webm');
				$this->formatFile(DS.$path, $name. $lang."_".$idWord, ".webm");
				return ($path .$name. $lang."_".$idWord);
			}
		}
		else{
			return $o_path;
		}
	}

	/*
	 * Fitxategi igoera kudeatzeko funtzioa
	*/

	function treatImgFile($name, $folder, $lang){
		$option=JRequest::getCmd('option');

		$o_path = JRequest::getString($name.'-vo');
		$path = 'components'.DS.$option.DS.'uploads'.DS.$folder.DS;

		if (($_FILES[$name]["size"] < 15000000) && ($_FILES[$name]["name"] != "")){

			if ($_FILES[$name]["error"] > 0){
				echo "Return Code: " . $_FILES[$i_name]["error"] . "<br />";
				return $o_path;
			}
			else{
				/*
				 echo "Upload: " . $_FILES["$i_name"]["name"] . "<br />";
				echo "Type: " . $_FILES["$i_name"]["type"] . "<br />";
				echo "Size: " . ($_FILES["$i_name"]["size"] / 1024) . " Kb<br />";
				echo "Temp file: " . $_FILES["$i_name"]["tmp_name"] . "<br />";
				*/

				if (file_exists($path . $lang.'.png')){
					chmod($path . $lang.'.png', 0644);
					unlink($path . $lang.'.png');
				}
				move_uploaded_file($_FILES[$name]["tmp_name"],
				$path . $lang.'.png');

				return ($path . $lang.'.png');
			}
		}
		else{
			return $o_path;
		}
	}

	function treatZipFile($name, $folder, $lang){
		$option=JRequest::getCmd('option');

		$o_path = JRequest::getString($name.'-vo');
		$path = 'components'.DS.$option.DS.'uploads'.DS.$folder.DS;

		if (($_FILES[$name]["size"] < 15000000) && ($_FILES[$name]["name"] != "")){

			if ($_FILES[$name]["error"] > 0){
				echo "Return Code: " . $_FILES[$i_name]["error"] . "<br />";
				return $o_path;
			}
			else{
				/*
				 echo "Upload: " . $_FILES["$i_name"]["name"] . "<br />";
				echo "Type: " . $_FILES["$i_name"]["type"] . "<br />";
				echo "Size: " . ($_FILES["$i_name"]["size"] / 1024) . " Kb<br />";
				echo "Temp file: " . $_FILES["$i_name"]["tmp_name"] . "<br />";
				*/

				if (file_exists($path . $lang.'-'.$name.'.zip')){
					chmod($path . $lang.'-'.$name.'.zip', 0644);
					unlink($path . $lang.'-'.$name.'.zip');
				}
				move_uploaded_file($_FILES[$name]["tmp_name"],
				$path . $lang.'-'.$name.'.zip');

				return ($path . $lang.'-'.$name.'.zip');
			}
		}
		else{
			return $o_path;
		}
	}

	/*
	 * Fitxategi igoera kudeatzeko funtzioa
	*/

	function treatAudioFile($name, $folder, $lang){
		$option=JRequest::getCmd('option');

		$o_path = JRequest::getString($name.'o');
		$path = 'components'.DS.$option.DS.'uploads'.DS.$folder.DS;

		if (($_FILES[$name]["size"] < 15000000) && ($_FILES[$name]["name"] != "")){

			if ($_FILES[$name]["error"] > 0){
				echo "Return Code: " . $_FILES[$i_name]["error"] . "<br />";
				return $o_path;
			}
			else{
				/*
				 echo "Upload: " . $_FILES["$i_name"]["name"] . "<br />";
				echo "Type: " . $_FILES["$i_name"]["type"] . "<br />";
				echo "Size: " . ($_FILES["$i_name"]["size"] / 1024) . " Kb<br />";
				echo "Temp file: " . $_FILES["$i_name"]["tmp_name"] . "<br />";
				*/

				if (file_exists($path . $lang.'_'.$name.'.webm')){
					chmod($path . $lang.'_'.$name.'.webm', 0644);
					unlink($path . $lang.'_'.$name.'.webm');
				}
				move_uploaded_file($_FILES[$name]["tmp_name"],
				$path . $lang.'_'.$name.'.webm');
				$this->formatFile($path . $lang.'_'.$name, ".webm");
				return ($path . $lang.'_'.$name);
			}
		}
		else{
			return $o_path;
		}
	}

	function treatPDFFile($name, $folder, $lang){
		$option=JRequest::getCmd('option');

		$o_path = JRequest::getString($name.'-vo');
		$path = 'components'.DS.$option.DS.'uploads'.DS.$folder.DS;

		if (($_FILES[$name]["size"] < 15000000) && ($_FILES[$name]["name"] != "")){

			if ($_FILES[$name]["error"] > 0){
				echo "Return Code: " . $_FILES[$i_name]["error"] . "<br />";
				return $o_path;
			}
			else{
				/*
				 echo "Upload: " . $_FILES["$i_name"]["name"] . "<br />";
				echo "Type: " . $_FILES["$i_name"]["type"] . "<br />";
				echo "Size: " . ($_FILES["$i_name"]["size"] / 1024) . " Kb<br />";
				echo "Temp file: " . $_FILES["$i_name"]["tmp_name"] . "<br />";
				*/
				if (file_exists($path . $lang.'-'.$name.'.pdf')){
					chmod($path . $lang.'-'.$name.'.pdf', 0644);
					unlink($path . $lang.'-'.$name.'.pdf');
				}
				
				move_uploaded_file($_FILES[$name]["tmp_name"],
				$path . $lang.'-'.$name.'.pdf');

				return ($path . $lang.'-'.$name.'.pdf');
			}
		}
		else{
			return $o_path;
		}
	}

	/*
	 * Hitz berri bat sartzeko funtzioa
	*/
	function addWord(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$dtoWord = $factoria->createDTOWord();
		$wDao=$factoria->getDAOWord();

		$idWord = JRequest::getString('idWord');
		if($idWord == ""){
			$idWord = null;
		}
		$dtoWord->setIdWord($idWord);
		$dtoWord->setWordEu(JRequest::getString('word_eu'));
		$dtoWord->setWordEs(JRequest::getString('word_es'));

		$active = JRequest::getBool('active');
		if(!$active){
			$active = 0;
		}
		$dtoWord->setActive($active);

		$w = $this->getWord();

		if($w->idWord == null || $w->idWord == ""){
				
			$result = $wDao->addWord($con, $dtoWord);
		}
		else{
				
			$result = $wDao->updateWord($con, $dtoWord);
		}

		return $result;
	}

	/*
	 * Hitz berri bat sartzeko funtzioa
	*/
	function addQa(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$dtoQa = $factoria->createDTOQa();
		$qaDao=$factoria->getDAOQa();

		$id = JRequest::getString('id');
		if($id == ""){
			$id = null;
		}
		$dtoQa->setId($id);
		$dtoQa->setQEu(JRequest::getString('q_eu'));
		$dtoQa->setQEs(JRequest::getString('q_es'));
		$dtoQa->setAEu(JRequest::getString('a_eu'));
		$dtoQa->setAEs(JRequest::getString('a_es'));

		$active = JRequest::getBool('active');
		if(!$active){
			$active = 0;
		}
		$dtoQa->setActive($active);

		$qa = $this->getQa();

		if($qa->id == null || $qa->id == ""){
				
			$result = $qaDao->addQa($con, $dtoQa);
		}
		else{
				
			$result = $qaDao->updateQa($con, $dtoQa);
		}

		return $result;
	}

	/*
	 * Hizkuntza berri bat sartzeko funtzioa
	*/
	function addLang(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$dtoLang = $factoria->createDTOLang();
		$lDao=$factoria->getDAOLang();

		$dtoLang->setIsoCode(JRequest::getString('lang_code'));
		$dtoLang->setNameEu(JRequest::getString('lang_eu'));
		$dtoLang->setNameEs(JRequest::getString('lang_es'));
		//$dtoLang->setTextEs(JRequest::getString('text_es'));
		//$dtoLang->setTextEu(JRequest::getString('text_eu'));

		$active = JRequest::getBool('active');
		if(!$active){
			$active = 0;
		}
		$dtoLang->setActive($active);

		$lang = $this->getLang();

		if($dtoLang->getIsoCode()== "" || $dtoLang->getIsoCode()== null){
			$dtoLang->setIsoCode($lang->getIsoCode());
		}

		$dtoLang->setMapPath($this->treatImgFile('lang_map', 'maps',$dtoLang->getIsoCode()));
		$dtoLang->setAlfPath($this->treatImgFile('lang_alf', 'alfs',$dtoLang->getIsoCode()));
		$dtoLang->setRscPath($this->treatZipFile('rsc', 'pkgs',$dtoLang->getIsoCode()));
		$dtoLang->setAudioPath($this->treatZipFile('audio', 'pkgs',$dtoLang->getIsoCode()));
		$dtoLang->setPdfPath($this->treatPDFFile('info', 'pdfs', $dtoLang->getIsoCode()));

		if($lang->iso_code == null || $lang->iso_code == ""){

			$result = $lDao->addLang($con, $dtoLang);
		}
		else{

			$result = $lDao->updateLang($con, $dtoLang);
		}

		return $result;

	}

	function addSong(){
		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$dtoSong = $factoria->createDTOSong();
		$sDao=$factoria->getDAOSong();

		$dtoSong->setIsoCode(JRequest::getString('lang_code'));
		$dtoSong->setId(JRequest::getString('id'));
		$dtoSong->setNameEu(JRequest::getString('name_eu'));
		$dtoSong->setNameEs(JRequest::getString('name_es'));
		$dtoSong->setSpelling(JRequest::getString('spelling'));
		$dtoSong->setPronoun(JRequest::getString('pronoun'));

		$active = JRequest::getBool('active');
		if(!$active){
			$active = 0;
		}
		$dtoSong->setActive($active);

		$lang = $this->getLang();

		if($dtoSong->getIsoCode()== "" || $dtoSong->getIsoCode()== null){
			$dtoSong->setIsoCode($lang->getIsoCode());
		}

		$dtoSong->setAudioPath($this->treatAudioFile("song$dtoSong->id-a", 'songs',$dtoSong->getIsoCode()));

		$result = $sDao->addSong($con, $dtoSong);

		return $result;

	}

	/*
	 * Hitz denak lortzeko funtzioa
	*/
	function getAllWords($active){

		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$wordDao=$factoria->getDAOWord();

		return ($wordDao->getWords($con, $active));
	}

	/*
	 * Hitz denak lortzeko funtzioa
	*/
	function getAllQas($active){

		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$qaDao=$factoria->getDAOQa();

		return ($qaDao->getQas($con, $active));
	}

	/*
	 * Hizkuntza denak lortzeko funtzioa
	*/
	function getAllLangs($c_lang, $active){

		$factoria=DAOFactoryImpl::getInstance();
		$con= $factoria->getConnection();

		$langDao=$factoria->getDAOLang();

		$orderby = " order by name_es";
		if($c_lang == 'eu-ES'){
			$orderby = " order by name_eu";
		}

		return ($langDao->getAllLangs($con, $orderby, $active));
	}

	/*
	 * Hiru argazki aleatorioki hartzeko funtzioa
	 */

	function getRandomImages(){
		$directory = 'components'.DS.'com_hizkuntzak'.DS.'img'.DS.'txikiak'.DS;
		$files = Array();
		$result = Array();
 		if ($handle = opendir(getcwd().DS.$directory)) {
		    while (false !== ($entry = readdir($handle))) {
		    	if (strpos($entry, '.jpg', 1)){
                    $files[]=$entry;
                } 
		    }

		    closedir($handle);
		}

		if(count($files) != 0){
			for($i = 0; $i < 3; $i++){
				$index = rand(0, count($files)-1);
				$img = $files[$index];
				
				while ($img == "" || $img == " "){
					$index = rand(0, count($files)-1);
					$img = $files[$index];
				}
				
				$result[] = $directory.$img;
				unset($files[$index]);
				$files = array_values($files);
			}
		}
		
		return $result;
	}

	/*
	 * Hizkuntza Textuak aukeratzeko funtzioa
	*/
	function selectLangRsc(){

		$lang = $this->checkLang();

		/*if($lang == "es-ES"){
			$result = new RscES();
		}
		else*/
		$result = new RscEU();

		return $result;

	}

	/*
	 * Momentuko hizkuntza ikusten duen funtzioa
	*/
	function checkLang(){
    	/*
    	$aux = $_SESSION['__default'];
		$aux = $aux['registry']->_registry;
		$aux = $aux['application'];
		$lang = $aux['data']->lang;
		
		return $lang;
		*/
		return "eu-ES";
    }

}
